import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import http from './assets/http.js'
import VueLazyload from 'vue-lazyload'
import {
  Button,
  Select,
  Checkbox,
  InputNumber,
  MessageBox,
  Message,
  Cascader,
  Icon,
  Pagination,
  Dropdown,
  Backtop,
} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';


// 配置项
Vue.use(VueLazyload, {
  loading: "https://img.zcool.cn/community/01dc1c587d86c1a8012060c8f0198c.gif",
})
// 第二参数
Vue.prototype.$http = http

Vue.config.productionTip = false
Vue.prototype.$msgbox = MessageBox
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$message = Message

Vue.use(Dropdown)
//让全局都可以用到这个组件
Vue.use(Button)
Vue.use(Checkbox)
Vue.use(InputNumber)
Vue.use(Cascader)
Vue.use(Icon)
Vue.use(Pagination)
Vue.use(Backtop)




// 路由守卫
router.beforeEach((to, from, next) => {
  let token = sessionStorage.getItem('token')
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    if (token) { //判断本地是否存在access_token
      next();
    } else {
      next('/login');
    }
  } else {
    next();
  }
  /*如果本地 存在 token 则 不允许直接跳转到 登录页面*/

});



new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')


// 自定义指令实现back-top;
Vue.directive('scroll-show', {
  inserted(el, binding) {
    let scope = binding.arg || '200';
    window.addEventListener('scroll', function (e) {
      if (this.scrollY > Number(scope)) {
        binding.value.value = true;
      } else {
        binding.value.value = false;
      }
    })
  }
})
Vue.directive('back-top', {
  inserted(el, binding) {
    let e = binding.arg || 'click';
    el.addEventListener(e, function () {
      let scrollToptimer = setInterval(function () {
        let top = document.documentElement.scrollTop + document.body.scrollTop;
        let speed = top / 4;
        top -= speed;
        document.documentElement.scrollTop = document.body.scrollTop = top;
        if (top == 0) {
          clearInterval(scrollToptimer);
        }
      }, 30)
    })
  }
})